# 水球图每个专业人数在占总人数的百分比。
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Grid, Liquid, Page
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType

from Page.sql import search_all

df = pd.DataFrame(search_all(), columns=['name', 'class'])  # 这里列出你想要的列名
group = df.groupby('class')['name'].count()  # class分类，统计name
print(group)
# 选取每个专业的人
xa = df[df['class'].str.startswith('信')]
rj = df[df['class'].str.startswith('软')]
jk = df[df['class'].str.startswith('计')]
yy = df[df['class'].str.startswith('医')]
total = len(df)
print(len(xa))
print(len(xa) / total)
l1 = (
    Liquid(init_opts=opts.InitOpts(width="1100px", height="1100px", theme=ThemeType.DARK))
    .add(
        "lq",
        [float(len(xa) / total)],
        center=["25%", "50%"],
        label_opts=opts.LabelOpts(
            font_size=50,
            formatter=JsCode(
                """function (param) {
                    return (Math.floor(param.value * 10000) / 100) + '%';
                }"""
            ),
            position="inside",
        ),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="多个 Liquid 显示"))
)

l2 = Liquid().add(
    "lq",
    [float(len(rj) / total)],
    center=["60%", "50%"],
    label_opts=opts.LabelOpts(
        font_size=50,
        formatter=JsCode(
            """function (param) {
                    return (Math.floor(param.value * 10000) / 100) + '%';
                }"""
        ),
        position="inside",
    ),
)

l3 = Liquid().add(
    "lq",
    [float(len(jk) / total)],
    center=["25%", "50%"],
    label_opts=opts.LabelOpts(
        font_size=50,
        formatter=JsCode(
            """function (param) {
                    return (Math.floor(param.value * 10000) / 100) + '%';
                }"""
        ),
        position="inside",
    ),
)

l4 = Liquid().add(
    "lq",
    [float(len(yy) / total)],
    center=["60%", "50%"],
    label_opts=opts.LabelOpts(
        font_size=50,
        formatter=JsCode(
            """function (param) {
                    return (Math.floor(param.value * 10000) / 100) + '%';
                }"""
        ),
        position="inside",
    ),
)

grid1 = Grid(init_opts=opts.InitOpts(width="1600px", height="800px", theme=ThemeType.DARK)) \
    .add(l1, grid_opts=opts.GridOpts()) \
    .add(l2, grid_opts=opts.GridOpts())

grid2 = Grid(init_opts=opts.InitOpts(width="1600px", height="800px", theme=ThemeType.DARK)) \
    .add(l3, grid_opts=opts.GridOpts()) \
    .add(l4, grid_opts=opts.GridOpts())

page = Page(interval=0)
page.add(
    grid1,
    grid2
)
page.render("水球图.html")
# grid.render("水球图.html")
